<?php

/**
 * Class Order_con 订单管理
 */
class Order_con extends Lxbase{

    /**
     * 预订单列表
     */
    public function beforeOrder(){
        $_where = ' where mold = 1 and (status = 1 or status = 4 or status = 5 or status = 6) and hid = ' . $_SESSION['hyid'];
        $pam = array();
        if(isset($_REQUEST['in_time']) && $_REQUEST['in_time']){
            $_where .= ' and in_time >= "' . $_REQUEST['in_time'] . '"';
            $pam['in_time'] = $_REQUEST['in_time'];
        }
        if(isset($_REQUEST['out_time']) && $_REQUEST['out_time']){
            $_where .= ' and in_time <= "' . $_REQUEST['out_time'] . '"';
            $pam['out_time'] = $_REQUEST['out_time'];
        }
        if(isset($_REQUEST['order_sn']) && $_REQUEST['order_sn']){
            $_where .= ' and order_sn = "' . $_REQUEST['order_sn'] . '"';
            $pam['order_sn'] = $_REQUEST['order_sn'];
        }
        if(isset($_REQUEST['create_name']) && $_REQUEST['create_name']){
            $_where .= ' and create_name = "' . $_REQUEST['create_name'] . '"';
            $pam['create_name'] = $_REQUEST['create_name'];
        }
        if(isset($_REQUEST['create_phone']) && $_REQUEST['create_phone']){
            $_where .= ' and create_phone = "' . $_REQUEST['create_phone'] . '"';
            $pam['create_phone'] = $_REQUEST['create_phone'];
        }
        if(isset($_REQUEST['status']) && $_REQUEST['status']){
            $_where .= ' and status = ' . $_REQUEST['status'];
            $pam['status'] = $_REQUEST['status'];
        }
        $parameter = array();
        $parameter[] = 'm=order';
        $parameter[] = 'a=beforeOrder';
        $parameter[] = 'in_time=' . $_REQUEST['in_time'];
        $parameter[] = 'out_time=' . $_REQUEST['out_time'];
        $parameter[] = 'order_sn=' . $_REQUEST['order_sn'];
        $parameter[] = 'create_name=' . $_REQUEST['create_name'];
        $parameter[] = 'create_phone=' . $_REQUEST['create_phone'];
        $parameter[] = 'status=' . $_REQUEST['status'];
        $page = isset($_GET['page'])?$_GET['page']:1;
        $pagesize = 20;
        $sort = " order by id desc";
        $limit = ' limit ' . (intval($page)-1)*$pagesize . ',' . $pagesize;
        $pagestr = $this->termpage('hotel_order',$_where . $sort,$parameter,$page,$pagesize);
        $res = $this->arrdata('*','hotel_order',$_where,'id desc',$limit);
        foreach($res as $k=>$v){
            $res[$k]['info'] = unserialize($v['info']);
            foreach($res[$k]['info'] as $a=>$b){
                $res[$k]['info'][$a]['name'] = $this->getTnameByid($b['tid']);
                $roomlist = explode(',',$b['rhao']);
                $res[$k]['info'][$a]['enter'] = $this->countRooms($roomlist);
            }
        }
        $this->smarty->assign('res',$res);
        $this->smarty->assign('types',$this->getTypesByHid());
        $this->smarty->assign('pagestr',$pagestr);
        $this->smarty->assign('pam',$pam);
        $this->smarty->assign('ostatus',$this->getconfig('hotel')['order_status']); //订单状态
        $this->smarty->display('reserve.tpl');
    }

    /**
     * 计算入住间数
     */
    public function countRooms($roomarr = array()){
        $i = 0;
        foreach($roomarr as $k=>$n){
            $res = $this->viewdata('id','hotel_room','where rtype = 2 and name = "' . $n . '" and uid = ' . $_SESSION['userinfo']['userid']);
            if($res){
                $i++;
            }
        }
        return $i++;
    }

    /**
     * 根据酒店id取出房型 Ajax
     */
    public function getTypesByHidAjax(){
        $hid = $_SESSION['hyid'];
        $res = $this->arrdata('*','hotel_type',' where hid = ' . (int)$hid);
        if($res){
            echo json_encode(array('status'=>1,msg=>$res));exit;
        }else{
            echo json_encode(array('status'=>0));exit;
        }
    }

    /**
     * 根据酒店id取出房型
     */
    public function getTypesByHid(){
        $hid = $_SESSION['hyid'];
        $res = $this->arrdata('*','hotel_type',' where hid = ' . (int)$hid);
        return $res;
    }

    /**
     * 根据房型id和酒店id取出方案
     */
    public function getProByTid(){
        $tid = $_REQUEST['tid'];
        $hid = $_SESSION['hyid'];
        $pid = $_REQUEST['pid'];
        $res = $this->arrdata('*','hotel_type_project',' where status = 1 and hid = ' . (int)$hid . ' and tid = ' . (int)$tid);
        if($res) {
            $_where = 'where rtype=1 and status = 1 and hid = ' . $hid . ' and tid = ' . $tid . ' and pid = ' . $res[0]['id'];
            $res2 = $this->numdata('hotel_room', $_where);
        }else{
            $res2 = 0;
        }
        echo json_encode(array('status'=>1,'msg'=>$res,'msg2'=>$res2));exit;
    }

    /**
     * 根据房型 酒店id 取出房间数
     */
    public function getRoomsNum(){
        $tid = $_REQUEST['tid'];
        $hid = $_SESSION['hyid'];
        $_where = 'where rtype=1 and hid = ' . $hid . ' and tid = ' . $tid;
        $res = $this->numdata('hotel_room',$_where);
        if($res){
            echo json_encode(array('status'=>1,msg=>$res));exit;
        }else{
            echo json_encode(array('status'=>0));exit;
        }
    }

    /**
     * 根据房型id获取房型名称
     */
    public function getTnameByid($id = 0){
        if(!$id){
            return '';exit;
        }
        $res = $this->viewdata('name','hotel_type','where id = ' . $id);
        if($res){
            return $res['name'];
        }else{
            return '';
        }
    }

    /**
     * 根据方案id获取方案名称
     */
    public function getPnameByid($id = 0){
    	$res = $this->viewdata('name','hotel_type_project','where id = ' . $id);
    	if($res){
    		return $res['name'];
    	}else{
    		return '';
    	}
    }
    
    /**
     * 保存个人预订订单
     */
    public function saveBeforePerson(){
        $data = array();
        $data['type'] = 1;
        $data['add_time'] = date('Y-m-d H:i:s',time());
        $data['up_time'] = date('Y-m-d H:i:s',time());
        $data['hid'] = $_SESSION['hyid'];
        $data['order_sn'] = "Y" . time() . rand(1111,9999);
        $data['status'] = 1; //预订
        $data['create_name'] = $_POST['create_name'];
        $data['create_phone'] = $_POST['create_phone'];
        $data['vip_num'] = $_POST['vip_num'];
        $data['in_time'] = $_POST['in_time'];
        $data['out_time'] = $_POST['out_time'];
        $data['liu_time'] = $_POST['liu_time'];
        $data['pay_type'] = $_POST['pay_type'];
        $data['remark'] = $_POST['remark'];
    //    $data['payment'] = $_POST['payment'];
        $data['mold'] = 1;
        $data['source'] = 2;
        $data['uid'] = '';
        $oinfo = array();
        $amount = 0;
        $i=0;
        foreach($_POST['rnum'] as $k=>$v){
            if($v!=='0'){
                $oinfo[$i]['tid'] = $_POST['tid'][$k];
                $oinfo[$i]['pid'] = $_POST['pid'][$k];
                $oinfo[$i]['prices'] = $_POST['prices'][$k];
                $oinfo[$i]['rnum'] = $_POST['rnum'][$k];
                $amount += $_POST['prices'][$k];
                $oinfo[$i]['rhao'] = $this->allotRoom($_POST['tid'][$k],$_POST['rnum'][$k]);
                $i++;
            }
        }
        $data['amount'] = $amount;
        $data['info'] = serialize($oinfo);
        $res = $this->setdata('hotel_order',$data);
        if($res){
            header('location:?m=order&a=beforeOrder');exit;
        }else{
            header('location:?m=order&a=beforeOrder');exit;
        }
    }

    /**
     * 保存团队预订订单
     */
    public function saveBeforeTeam(){
        $data = array();
        $data['type'] = 2;
        $data['add_time'] = date('Y-m-d H:i:s',time());
        $data['up_time'] = date('Y-m-d H:i:s',time());
        $data['hid'] = $_SESSION['hyid'];
        $data['order_sn'] = "TY" . time() . rand(1111,9999);
        $data['status'] = 1;  //预订
        $data['create_name'] = $_POST['create_name'];
        $data['create_phone'] = $_POST['create_phone'];
        $data['vip_num'] = $_POST['vip_num'];
        $data['in_time'] = $_POST['in_time'];
        $data['out_time'] = $_POST['out_time'];
        $data['liu_time'] = $_POST['liu_time'];
        $data['pay_type'] = $_POST['pay_type'];
        $data['remark'] = $_POST['remark'];
    //    $data['payment'] = $_POST['payment'];
        $data['team_info'] = $_POST['teams'][0] . ',' . $_POST['teams'][1] . ',' . $_POST['teams'][2];
        $data['mold'] = 1;
        $data['source'] = 2;
        $data['uid'] = '';
        $oinfo = array();
        $amount = 0;
        $i=0;
        foreach($_POST['rnum'] as $k=>$v){
            if($v!=='0'){
                $oinfo[$i]['tid'] = $_POST['tid'][$k];
                $oinfo[$i]['pid'] = $_POST['pid'][$k];
                $oinfo[$i]['prices'] = $_POST['prices'][$k];
                $oinfo[$i]['rnum'] = $_POST['rnum'][$k];
                $amount += $_POST['prices'][$k];
                $oinfo[$i]['rhao'] = $this->allotRoom($_POST['tid'][$k],$_POST['rnum'][$k]);
                $i++;
            }
        }
        $data['amount'] = $amount;
        $data['info'] = serialize($oinfo);
        $res = $this->setdata('hotel_order',$data);
        if($res){
            header('location:?m=order&a=beforeOrder');exit;
        }else{
            header('location:?m=order&a=beforeOrder');exit;
        }
    }

    /**
     * 计算房间价格
     */
    public function getPrices(){
        $data = array();
        $data['in_time'] = $_REQUEST['in_time'];// 预入时间
        $data['out_time'] = $_REQUEST['out_time'];//离店时间
        $data['tid'] = $_REQUEST['tid'];//房型id
        $data['pid'] = $_REQUEST['pid'];//方案id
        $data['num'] = $_REQUEST['num'];//房间数
        if($data['in_time'] &&  $data['out_time'] && $data['num']) {
            $days = $this->getDays($data['in_time'], $data['out_time']);
            //根据方案id获取三种价格
            $res = $this->viewdata('pacific_price,weekend_price,special_price', 'hotel_type_project', 'where id = ' . $data['pid']);
            $prices = ($days['jnum'] * $res['pacific_price'] + $days['mnum'] * $res['weekend_price'] + $days['tnum'] * $res['special_price']) * (int)$data['num'];
        }else{
            $prices = 0;
        }
        $_where = 'where rtype=1 and status = 1 and hid = ' . $_SESSION['hyid'] . ' and tid = ' . (int)$data['tid'] . ' and pid = ' . (int)$data['pid'];
        $res2 = $this->numdata('hotel_room',$_where);
        echo json_encode(array('prices'=>$prices,'rnum'=>$res2));exit;
    }

    /**
     * 根据时间段，获取周末、平常日、特殊日天数
     */
    public function getDays($stime='',$etime=''){
        $sday = date('j',strtotime($stime)); //开始时间号数
        $eday = date('j',strtotime($etime)); //结束时间号数
        $smon =  date('n',strtotime($stime)); //开始时间月数
        $emon =  date('n',strtotime($etime)); //结束时间月数
        if($smon == $emon){//同月
            $daynum = $eday-$sday; //天数
        }else{//不同月
            $days = date('t',strtotime($stime));//算出本月天数
            $daynum = $days-$sday+$eday;
        }
        $jobs = array('1','2','3','4','7'); //工作日
        $mos = array('5','6');//周末
        $stimestamp = strtotime($stime);
        $jnum = $mnum = $tnum = 0;
        $res = $this->viewdata('*','hotel_times','where hid = ' . $_SESSION['hyid']);
        $tarray = explode(',',$res['times']);
        foreach($tarray as $k=>$v){
            $tarray[$k] = strtotime($v);
        }
        for($i=0;$i<$daynum;$i++){
            if(in_array($stimestamp,$tarray)) {
                $tnum++;
                $stimestamp = $stimestamp + 86400;
                continue;
            }
            if(in_array(date('N',$stimestamp),$jobs)){
                $jnum++;
            }else{
                $mnum++;
            }
            $stimestamp = $stimestamp+86400;
        }
        return array('jnum'=>$jnum,'mnum'=>$mnum,'tnum'=>$tnum);
    }

    /**
     * 根据酒店、房型 进行分房
     */
    public function allotRoom($tid=0,$num=0){
        $hid = $_SESSION['hyid'];
        $res = $this->arrdata('id,name','hotel_room','where rtype = 1 and status = 1 and hid = ' . $hid . ' and tid = ' . $tid,'','limit 0,' . (int)$num);
        $names = $ids = '';
        foreach($res as $k=>$v){
            $names .= $v['name'] . ',';
            $ids .= $v['id'] . ',';
        }
        $names = rtrim($names,',');
        $ids = rtrim($ids,',');
        $this->updata('hotel_room','rtype=3','where id in (' . $ids . ')');
        return $names;
    }

    /**
     *  直接下订单
     */
    public function directOrder(){
        $data = array();
        $data['hid'] = $_SESSION['hyid'];
        $data['uid'] = '';
        $data['create_name'] = '';
        $data['create_phone'] = $_POST['create_phone'];
        $data['team_info'] = '';
        $data['liu_time'] = '0000-00-00 00:00:00';
        $data['remark'] = $_POST['remark'];
        $data['in_time'] = $_POST['in_time'];
        $data['out_time'] = $_POST['out_time'];
        $data['amount'] = $_POST['amount'];
        $data['info'] = array();
        $data['info'][0]['tid'] = $_POST['tid'];
        $data['info'][0]['pid'] = $_POST['proid'];
        $data['info'][0]['prices'] = $_POST['amount'];
        $data['info'][0]['rnum'] = 1;
        $data['info'][0]['rhao'] = $_POST['rhao'];
        $temparr = array();
        $namelist = '';
        foreach($_POST['pername'] as $a=>$b){
        	$temparr[$a]['name'] = $b;
            $namelist .= $b . '   ';
        	$temparr[$a]['sex'] = $_POST['persex'][$a];
        	$temparr[$a]['birth'] = $_POST['brithday'][$a];
        	$temparr[$a]['idnum'] = $_POST['pernum'][$a];
        }
        $data['info'][0]['userinfo']['name'] = $namelist;
        $data['info'] = serialize($data['info']);
        $data['vip_num'] = $_POST['vip_num'];
        $data['pay_type'] = $_POST['pay_type'];
        $data['cash'] = $_POST['cash']?$_POST['cash']:0;
        $data['status'] = 2; //入住
        $data['source'] = 2; //入住
        $data['add_time'] = date('Y-m-d H:i:s',time());
        $data['up_time'] = date('Y-m-d H:i:s',time());
        $data['order_sn'] = 'D' . time() . rand(1111,9999);
        $data['type'] = 1;
        $data['mold'] = 2;
        $res = $this->setdata('hotel_order',$data);
        if($res){
            //添加住房记录
            $logarr = array();
            $logarr['order_id'] = (int)$res;
            $logarr['room_id'] = (int)$_POST['room_id'];
            $logarr['userinfo'] = serialize($temparr);
            $logarr['status'] = 2;
            $logarr['add_time'] = date('Y-m-d H:i:s',time());
            $this->setdata('hotel_room_logs',$logarr);
            //修改房间状态
            $this->setRoomEmpty($_POST['rhao'],2);
            header('location:?m=manage&a=index');exit;
        }else{
        	header('location:?m=manage&a=index');exit;
        }
    }

    /**
     * 修改房间状态
     */
    public function setRoomStatus($id=0,$stat=0){
        $this->updata('hotel_room','rtype = ' . $stat,'where id = ' . $id);
        return;
    }

    /**
     * 根据订单id，获取订单信息
     */
    public function getInfoById(){
        $id = $_REQUEST['id'];
        $res = $this->viewdata('*','hotel_order','where id = ' . $id);
        $this->smarty->assign('res',$res);
        $sday = date('j',strtotime($res['in_time'])); //开始时间号数
        $eday = date('j',strtotime($res['out_time'])); //结束时间号数
        $smon =  date('n',strtotime($res['in_time'])); //开始时间月数
        $emon =  date('n',strtotime($res['out_time'])); //结束时间月数
        if($smon == $emon){//同月
            $daynum = $eday-$sday+1; //天数
        }else{//不同月
            $days = date('t',strtotime($res['in_time']));//算出本月天数
            $daynum = $days-$sday+1+$eday;
        }
        $this->smarty->assign('daynum',$daynum);
        $uinfo = unserialize($res['info']);
        $roomnum = 0;
        $roomtypestr = '';
        foreach($uinfo as $m=>$n){
            //$roomnum += (int)$n['rnum'];  //getRoomStat
            $rhaoarr = array();
            $rhaoarr = explode(',',$n['rhao']);
            foreach($rhaoarr as $j=>$l){
                if($this->getRoomStat($l)=='2'){
                    $roomnum++;
                }
            }
            $roomtypestr .= $this->getTnameByid($n['tid']) . '  ';
        }
        $pid = $uinfo[0]['pid'];
        $p_pay_type = $this->viewdata('pay_type','hotel_type_project','where id = ' . $pid)['pay_type'];
        $this->smarty->assign('p_pay_type',$p_pay_type);
        $this->smarty->assign('per_info',$uinfo[0]['userinfo']['name']);
        $this->smarty->assign('roomtypestr',$roomtypestr);
        $this->smarty->assign('roomnum',$roomnum);
        $this->smarty->assign('hinfo',$this->getHotelInfoByid($_SESSION['hyid']));
        $this->smarty->assign('pay_type',$this->getconfig('hotel')['pay_type']); //支付方式
        $this->smarty->display('order_information.tpl');
    }
    
    /**
     * 根据时间段，算天数
     */
    public function getDayNum(){
    	$sday = date('j',strtotime($_POST['in_time'])); //开始时间号数
    	$eday = date('j',strtotime($_POST['out_time'])); //结束时间号数
    	$smon =  date('n',strtotime($_POST['in_time'])); //开始时间月数
    	$emon =  date('n',strtotime($_POST['out_time'])); //结束时间月数
    	if($smon == $emon){//同月
    		$daynum = $eday-$sday; //天数
    	}else{//不同月
    		$days = date('t',strtotime($_POST['in_time']));//算出本月天数
    		$daynum = $days-$sday+$eday;
    	}
    	echo $daynum;exit;
    }
    

    /**
     * 根据酒店Id获取酒店信息
     */
    public function getHotelInfoByid($hid=0){
        return $this->viewdata('*','hotel','where id = ' . $hid);
    }

    /**
     * 预订转订单
     */
    public function convertOrder(){
        $data = array();
        $data['remark'] = $_POST['remark'];
        $data['in_time'] = $_POST['in_time'];
        $data['type'] = $_POST['r_type'];
        $data['out_time'] = $_POST['out_time'];
        $data['create_name'] = $_POST['create_name'];
        $data['create_phone'] = $_POST['create_phone'];
        $data['pay_type'] = $_POST['pay_type'];
        $data['cash'] = $_POST['cash'];
    //    $data['payment'] = $_POST['payment'];
        $data['status'] = 2; //入住
        $data['mold'] = 2;
        $amount = 0;
        $infoarr = array();
        $i = 0;
        $rhaostr = '';
        foreach($_POST['prices'] as $b=>$a){
            $infoarr[$i]['tid'] = $_POST['tid'][$b];
            $infoarr[$i]['pid'] = $_POST['pid'][$b];
            $infoarr[$i]['prices'] = $_POST['prices'][$b];
            $infoarr[$i]['rnum'] = $_POST['rnum'][$b];
            $infoarr[$i]['rhao'] = $_POST['rhao'][$b];
            $rhaostr .= $_POST['rhao'][$b] . ',';
            $userinfo = array();
            $namestr = $_POST['name_'.($b+1)];
            $namearr = explode(',',$namestr);
            $sexstr = $_POST['sex_'.($b+1)];
            $sexarr = explode(',',$sexstr);
            $birthstr = $_POST['birth_'.($b+1)];
            $birtharr = explode(',',$birthstr);
            $idnumstr = $_POST['idnum_'.($b+1)];
            $idnumarr = explode(',',$idnumstr);
            $x = 0;
            foreach($namearr as $m=>$n){
                $userinfo[$x]['name'] = $n;
                $userinfo[$x]['sex'] = $sexarr[$m];
                $userinfo[$x]['birth'] = $birtharr[$m];
                $userinfo[$x]['idnum'] = $idnumarr[$m];
                $x++;
            }
            $infoarr[$i]['userinfo'] = $userinfo;
            $amount += $a;
            $i++;
        }
        $data['info'] = serialize($infoarr);
        $data['amount'] = $amount;
        if($data['type']==2){
            $data['team_info'] = $_POST['team_name'] . ',' . $_POST['team_person'] . ',' . $_POST['team_phone'];
            $data['order_sn'] = str_replace('TY','TD',$_POST['order_sn']);
        }else{
            $data['order_sn'] = str_replace('Y','D',$_POST['order_sn']);
        }
        $res = $this->updata('hotel_order',$data,'where id = ' . (int)$_POST['order_id']);
        if($res){
            //修改房间状态....
            $r_hao_arr = explode(',',rtrim($rhaostr,','));
            foreach($r_hao_arr as $g=>$h){
                $this->setRoomEmpty($h,2);
            }
            header('location:?m=order&a=beforeOrder');exit;
        }else{
            header('location:?m=order&a=beforeOrder');exit;
        }
    }

    /**
     * 将房间状态设为空房或打扫  Ajax
     */
    public function setRoomEmptyAjax(){
        $id = $_REQUEST['id'];
        $stat = $_REQUEST['status'];
        $res = $this->updata('hotel_room','rtype = ' . $stat,'where id = ' . $id);
        if($res){
            echo json_encode(array('status'=>1,'msg'=>'设置成功'));
        }else{
            echo json_encode(array('status'=>0,'msg'=>'设置失败'));
        }
    }

    /**
     * 将房间状态设为空房或打扫
     */
    public function setRoomEmpty($name='',$rtype=0){
        return $this->updata('hotel_room','rtype = ' . $rtype,"where name = '" . $name . "' and hid = " . $_SESSION['hyid']);
    }

    /**
     * 取消预订单
     */
    public function cancelOrder(){
        $id = $_REQUEST['id'];
        // 将预订的房间的状态设为 1 空房 ...
        $orderinfo = unserialize($this->viewdata('info','hotel_order','where id = ' . $id)['info']);
        foreach($orderinfo as $k=>$v){
            $haoarr = array();
            $haoarr = explode(',',$v['rhao']);
            foreach($haoarr as $m=>$n){
                $this->setRoomEmpty($n,1);
            }
        }
        $res = $this->updata('hotel_order','status = 3','where id = ' . $id); //将预订单状态设为3：关闭
        if($res){
            echo json_encode(array('stat'=>1,'msg'=>'取消订单成功'));
        }else{
            echo json_encode(array('stat'=>0,'msg'=>'取消订单失败'));
        }
    }

    /**
     * 获取订单列表
     */
    public function orderlist(){
        $hid = $_SESSION['hyid'];
        $_where = ' where hid = ' . $hid;
        $pam = array();
        if(isset($_REQUEST['in_time']) && $_REQUEST['in_time']){
            $_where .= ' and in_time >= "' . $_REQUEST['in_time'] . '"';
            $pam['in_time'] = $_REQUEST['in_time'];
        }
        if(isset($_REQUEST['out_time']) && $_REQUEST['out_time']){
            $_where .= ' and in_time <= "' . $_REQUEST['out_time'] . '"';
            $pam['out_time'] = $_REQUEST['out_time'];
        }
        if(isset($_REQUEST['order_sn']) && $_REQUEST['order_sn']){
            $_where .= ' and order_sn = "' . $_REQUEST['order_sn'] . '"';
            $pam['order_sn'] = $_REQUEST['order_sn'];
        }
        if(isset($_REQUEST['create_name']) && $_REQUEST['create_name']){
            $_where .= ' and create_name = "' . $_REQUEST['create_name'] . '"';
            $pam['create_name'] = $_REQUEST['create_name'];
        }
        if(isset($_REQUEST['create_phone']) && $_REQUEST['create_phone']){
            $_where .= ' and create_phone = "' . $_REQUEST['create_phone'] . '"';
            $pam['create_phone'] = $_REQUEST['create_phone'];
        }
        if(isset($_REQUEST['status']) && $_REQUEST['status']){
            $_where .= ' and status = ' . $_REQUEST['status'];
            $pam['status'] = $_REQUEST['status'];
        }
        $parameter = array();
        $parameter[] = 'm=order';
        $parameter[] = 'a=orderlist';
        $parameter[] = 'in_time=' . $_REQUEST['in_time'];
        $parameter[] = 'out_time=' . $_REQUEST['out_time'];
        $parameter[] = 'order_sn=' . $_REQUEST['order_sn'];
        $parameter[] = 'create_name=' . $_REQUEST['create_name'];
        $parameter[] = 'create_phone=' . $_REQUEST['create_phone'];
        $parameter[] = 'status=' . $_REQUEST['status'];
        $page = isset($_GET['page'])?$_GET['page']:1;
        $pagesize = 20;
        $sort = " order by id desc";
        $limit = ' limit ' . (intval($page)-1)*$pagesize . ',' . $pagesize;
        $pagestr = $this->termpage('hotel_order',$_where . $sort,$parameter,$page,$pagesize);
        $res = $this->arrdata('*','hotel_order',$_where,'id desc',$limit);
        foreach($res as $k=>$v){
            $res[$k]['info'] = unserialize($v['info']);
            foreach($res[$k]['info'] as $a=>$b){
                $res[$k]['info'][$a]['name'] = $this->getTnameByid($b['tid']);
                $roomlist = explode(',',$b['rhao']);
                $res[$k]['info'][$a]['enter'] = $this->countRooms($roomlist);
            }
        }
        $this->smarty->assign('ostatus',$this->getconfig('hotel')['order_status']); //订单状态
        $this->smarty->assign('res',$res);
        $this->smarty->assign('pagestr',$pagestr);
        $this->smarty->assign('pam',$pam);
        $this->smarty->display('roomorder.tpl');
    }
    
    /**
     * 预订转入住，获取订单详情
     */
    public function getOrderDir(){
    	$id = $_GET['id'];
    	$res = $this->viewdata('*','hotel_order','where id = ' . (int)$id);
    	$res['pay_type_name'] = $this->getconfig('hotel')['pay_type'][$res['pay_type']];
    	$uinfo = unserialize($res['info']);
    	foreach($uinfo as $k=>$v){
    		$uinfo[$k]['tname'] = $this->getTnameByid($v['tid']);
    		$uinfo[$k]['pname'] = $this->getPnameByid($v['pid']);
    	}
    	if($res['type']=='2'){
    		$teaminfo = explode(',',$res['team_info']);
    		$res['team_name'] = $teaminfo[0];
    		$res['team_per'] = $teaminfo[1];
    		$res['team_phone'] = $teaminfo[2];
    	}
    	$msg = array();
    	$msg['info'] = $res;
    	$msg['uinfo'] = $uinfo;
    	echo json_encode(array('stat'=>1,'msg'=>$msg));exit;
    }

    /**
     * 预订转入住
     */
    public function setCheckIn()
    {
        $id = $_REQUEST['id'];
        $res = $this->viewdata('*','hotel_order','where id = ' . $id);
        $res['pay_type_name'] = $this->getconfig('hotel')['pay_type'][$res['pay_type']];
        $uinfo = unserialize($res['info']);
        $pid = $uinfo[0]['pid'];
        $p_pay_type = $this->viewdata('pay_type','hotel_type_project','where id = ' . $pid)['pay_type'];
        $this->smarty->assign('p_pay_type',$p_pay_type);
        $i = 0;
        $roominfo = array();
        foreach($uinfo as $m=>$n){
            $uinfo[$m]['typename'] = $this->getTnameByid($n['tid']);
            foreach(explode(',',$n['rhao']) as $a=>$b){
                $roominfo[$i] = $this->getRoomInfo($b);
                $roomres = $this->getCheckTime($res['id'],$roominfo[$i]['id']);
                if($roomres){
                    $roominfo[$i]['checktime'] = $roomres['add_time'];
                }else{
                    $roominfo[$i]['checktime'] = '';
                }
                $roominfo[$i]['typename'] = $uinfo[$m]['typename'];
                $i++;
            }
        }
        $this->smarty->assign('roominfo',$roominfo);
        $this->smarty->assign('hinfo',$this->getHotelInfoByid($_SESSION['hyid']));
        $this->smarty->assign('orderinfo',$res);
        $this->smarty->display('orderdetail.tpl');
    }

    /**
     * 根据房间号和酒店Id查出酒店信息
     */
    public function getRoomInfo($name=''){
        return $this->viewdata('*','hotel_room','where hid = ' . $_SESSION['hyid'] . ' and name = "' . $name . '"');
    }

    /**
     * 根据房间id和酒店Id查出房间信息
     */
    public function getRoomInfoByid(){
        $id = (int)$_POST['id'];
        $in_time = $_POST['in_time'];
        $out_time = $_POST['out_time'];
        $sday = date('j',strtotime($in_time)); //开始时间号数
        $eday = date('j',strtotime($out_time)); //结束时间号数
        $smon =  date('n',strtotime($in_time)); //开始时间月数
        $emon =  date('n',strtotime($out_time)); //结束时间月数
        if($smon == $emon){//同月
            $daynum = $eday-$sday; //天数
        }else{//不同月
            $days = date('t',strtotime($_POST['in_time']));//算出本月天数
            $daynum = $days-$sday+$eday;
        }
        $res = $this->viewdata('*','hotel_room','where id = ' . $id);
        $days = $this->getDays($in_time,$out_time);
        //根据方案id获取三种价格
        $res2 = $this->viewdata('pacific_price,weekend_price,special_price','hotel_type_project','where id = '.$res['pid']);
        $prices = $days['jnum']*$res2['pacific_price']+$days['mnum']*$res2['weekend_price']+$days['tnum']*$res2['special_price'];
        $res['daynum'] = $daynum;
        $res['prices'] = $prices;
        $res['tname'] = $this->getTnameByid($res['tid']);
        $res['pname'] = $this->getProName($res['pid']);
        echo json_encode($res);exit;
    }

    /**
     * 根据房间Id和订单Id 获取入住信息
     */
    public function getCheckInInfo(){
        $room_id = (int)$_POST['room_id'];
        $order_id = (int)$_POST['order_id'];
        $in_time = $_POST['in_time'];
        $out_time = $_POST['out_time'];
        $in_time = $_POST['in_time'];
        $out_time = $_POST['out_time'];
        $sday = date('j',strtotime($in_time)); //开始时间号数
        $eday = date('j',strtotime($out_time)); //结束时间号数
        $smon =  date('n',strtotime($in_time)); //开始时间月数
        $emon =  date('n',strtotime($out_time)); //结束时间月数
        if($smon == $emon){//同月
            $daynum = $eday-$sday; //天数
        }else{//不同月
            $days = date('t',strtotime($_POST['in_time']));//算出本月天数
            $daynum = $days-$sday+$eday;
        }
        $res = $this->viewdata('*','hotel_room','where id = ' . $room_id);
        $days = $this->getDays($in_time,$out_time);
        //根据方案id获取三种价格
        $res2 = $this->viewdata('pacific_price,weekend_price,special_price','hotel_type_project','where id = '.$res['pid']);
        $prices = $days['jnum']*$res2['pacific_price']+$days['mnum']*$res2['weekend_price']+$days['tnum']*$res2['special_price'];
        $res['daynum'] = $daynum;
        $res['prices'] = $prices;
        $res['tname'] = $this->getTnameByid($res['tid']);
        $res['pname'] = $this->getProName($res['pid']);
        $userinfo = unserialize($this->viewdata('userinfo','hotel_room_logs','where order_id = ' . $order_id . ' and room_id = ' . $room_id)['userinfo']);
        $res['userinfo'] = $userinfo;
        echo json_encode($res);exit;
    }

    /**
     * 根据方案Id取出方案名称
     */
    public function getProName($id=0){
        $res = $this->viewdata('name','hotel_type_project','where id = ' . $id);
        return $res['name'];
    }

    /**
     * 根据订单id和房间id获取入住时间
     */
    public function getCheckTime($order_id=0,$room_id=0){
        $res = $this->viewdata('add_time','hotel_room_logs','where order_id = ' . $order_id . ' and room_id = ' . $room_id);
        return $res;
    }

    /**
     * 取消房间
     */
    public function cancelRoom(){
        $room_id = $_GET['room_id'];
        $order_id = $_GET['order_id'];
        $roomarr = $this->getRoomsByOid($order_id);
        $this->updata('hotel_room','rtype=1','where id = ' . $room_id);
        $roomname = $this->viewdata('name','hotel_room','where id = ' . $room_id)['name'];
        $order_stat = 2;  //全部入住
        foreach($roomarr as $k=>$v){
            if($this->getRoomStat($v) == '3' && $v!=$roomname){
                //有预订的话，订单状态设为部分入住
                $order_stat = 6;
            }
        }
        if($order_stat==2){
            $setarr = array();
            $setarr['status'] = 2;
            $setarr['mold'] = 2;
            $this->updata('hotel_order',$setarr,'where id = ' . $order_id); //更新订单状态
        }else{
            $this->updata('hotel_order','status = ' . $order_stat,'where id = ' . $order_id); //更新订单状态
        }
        echo json_encode(array('stat'=>1,'msg'=>'退房成功'));exit;
    }

    /**
     * 单个退房
     */
    public function exitroom(){
        $room_id = $_GET['room_id'];
        $order_id = $_GET['order_id'];
        $roomarr = $this->getRoomsByOid($order_id);
        $this->updata('hotel_room','rtype=1','where id = ' . $room_id);
        $roomname = $this->viewdata('name','hotel_room','where id = ' . $room_id)['name'];
        foreach($roomarr as $k=>$v){
            if(($this->getRoomStat($v) == '3' || $this->getRoomStat($v) == '2')  && $v!=$roomname){
                echo json_encode(array('stat'=>1,'msg'=>'退房成功'));exit;
            }
        }
        $setarr = array();
        $setarr['status'] = 7;
        $setarr['mold'] = 2;
        $this->updata('hotel_order',$setarr,'where id = ' . $order_id); //更新订单状态
        echo json_encode(array('stat'=>1,'msg'=>'退房成功'));exit;
    }

    /**
     * 房间转入住
     */
    public function roomCheckIn(){
        $data = array();
        $data['order_id'] = $_POST['c_order_id'];
        $data['room_id'] = $_POST['c_room_id'];
        $data['status'] = 2;
        $data['add_time'] = date('Y-m-d H:i:s',time());
        $infoarr = array();
        $namelist = '';
        foreach($_POST['pername'] as $m=>$n){
            $infoarr[$m]['name'] = $n;
            $namelist .= $n . '  ';
            $infoarr[$m]['sex'] = $_POST['persex'][$m];
            $infoarr[$m]['birth'] = $_POST['brithday'][$m];
            $infoarr[$m]['idnum'] = $_POST['pernum'][$m];
        }
        $namelist = rtrim($namelist,'  ');
        $orderinfo = unserialize($this->viewdata('info','hotel_order','where id = ' . $data['order_id'])['info']);
        $orderinfo[0]['userinfo']['name'] .= '  ' . $namelist;
        $temparr = array();
        $temparr['info'] = serialize($orderinfo);
        $this->updata('hotel_order',$temparr,'where id = ' . $data['order_id']);
        $data['userinfo'] = serialize($infoarr);
        $res = $this->setdata('hotel_room_logs',$data);
        $this->setRoomEmpty($_POST['rhao'],2); //设置房间状态为入住
        $roomarr = $this->getRoomsByOid($data['order_id']);
        $order_stat = 2;  //全部入住
        foreach($roomarr as $k=>$v){
            if($this->getRoomStat($v) == '3' && $v!=$_POST['rhao']){
                //有预订的话，订单状态设为部分入住
                $order_stat = 6;
            }
        }
        if($order_stat==2){
            $setarr = array();
            $setarr['status'] = 2;
            $setarr['mold'] = 2;
            $old_sn = $this->viewdata('order_sn','hotel_order','where id = ' . $data['order_id'])['order_sn'];
            $setarr['order_sn'] = str_replace('Y','D',$old_sn);
            $this->updata('hotel_order',$setarr,'where id = ' . $data['order_id']); //更新订单状态
        }else{
            $this->updata('hotel_order','status = ' . $order_stat,'where id = ' . $data['order_id']); //更新订单状态
        }
        header('location:?m=order&a=setCheckIn&id=' . $data['order_id']);exit;
    }

    /**
     *  根据房间号码，取出房间状态
     */
    public function getRoomStat($room_num=''){
        return $this->viewdata('rtype','hotel_room','where name = "'.$room_num.'" and uid=' . $_SESSION['userinfo']['userid'])['rtype'];
    }

    /**
     * 根据订单id 取出所有预订房间
     */
    Public function getRoomsByOid($order_id=0){
        $res = $this->viewdata('info','hotel_order','where id = ' . $order_id);
        $info = unserialize($res['info']);
        $roomstr = '';
        foreach($info as $m=>$n){
            $roomstr .= $n['rhao'] . ',';
        }
        $roomarr = array();
        $roomarr = explode(',',rtrim($roomstr,','));
        return $roomarr;
    }

    /**
     * 全部退房
     */
    public function allexit(){
        $order_id = $_GET['id'];
        $roomarr = $this->getRoomsByOid($order_id);
        foreach($roomarr as $k=>$v){
            $this->setRoomEmpty($v,1); //设置房间状态为空房
        }
        $res = $this->updata('hotel_order','status=7','where id = ' . $order_id);
        if($res){
            echo json_encode(array('stat'=>1,'msg'=>'退房成功'));exit;
        }else{
            echo json_encode(array('stat'=>0,'msg'=>'退房失败'));exit;
        }
    }

    /**
     * 设置订单押金
     */
    public function setcash(){
        $order_id = $_POST['order_id'];
        $data = array();
        $data['cash'] = $_POST['cash'];
        $res = $this->updata('hotel_order',$data,'where id = ' . $order_id);
        if($res){
            echo json_encode(array('stat'=>1,'msg'=>'设置成功'));exit;
        }else{
            echo json_encode(array('stat'=>0,'msg'=>'设置失败'));exit;
        }
    }

}

